#ifndef __SUBSYSTEM_CAMPFIRE_BLOCK_BEHAVIOR_H__
#define __SUBSYSTEM_CAMPFIRE_BLOCK_BEHAVIOR_H__

#include "Subsystems/BlockBehaviors/SubsystemBlockBehavior.hpp"
#include <map>
#include <set>
#include <vector>
#include <mutex>
#include "GameLock.hpp"

namespace PocketSurvival
{
    class SubsystemTerrain;
    class SubsystemWeather;
    class SubsystemTimeOfDay;
    class SubsystemExplosions;

    // 篝火行为，包括添柴和燃烧缩减
    class SubsystemCampfireBlockBehavior : public SubsystemBlockBehavior
    {
    protected:
        SubsystemTerrain *m_terrain;
        SubsystemWeather *m_weather;
        SubsystemTimeOfDay *m_timeOfDay;
        SubsystemExplosions *m_explosions;

        std::set<Point3> m_campfireSet;
        SpinLock m_campfireSetLock;

        std::vector<Point3> m_toReduce;

    public:
        SubsystemCampfireBlockBehavior();

        void addCampfire(int32_t value, int32_t x, int32_t y, int32_t z);
        void removeCampfire(int32_t x, int32_t y, int32_t z);
        bool addFuel(int32_t x, int32_t y, int32_t z, int32_t value, int32_t count);
        inline bool addFuel(const Point3& point, int32_t value, int32_t count);

        bool resizeCampfire(int32_t x, int32_t y, int32_t z, int32_t steps);
        

        virtual void onNeighborBlockChanged(int32_t x, int32_t y, int32_t z, int32_t neighborX, int32_t neighborY, int32_t neighborZ) override;
        virtual void onChunkDiscarding(TerrainChunk *chunk) override;
        virtual bool onInteract(const TerrainRaycastResult &raycastResult, ComponentMiner *componentMiner) override;
        virtual void onHitByProjectile(const CellFace &cellFace, WorldItem *worldItem) override;
        virtual void onBlockAdded(int32_t value, int32_t oldValue, int32_t x, int32_t y, int32_t z) override;
        virtual void onBlockRemoved(int32_t value, int32_t newValue, int32_t x, int32_t y, int32_t z) override;
        virtual void onBlockModified(int32_t value, int32_t oldValue, int32_t x, int32_t y, int32_t z) override;

        virtual void update(float dt) override;
        virtual void load(const nlohmann::json &json) override;
        virtual void save(nlohmann::json& json) override;
        virtual const std::string& getName() const override;
        virtual SubUpdateType getUpdateType() override;
    };

    inline bool SubsystemCampfireBlockBehavior::addFuel(const Point3& point, int32_t value, int32_t count)
    {
        return addFuel(point.posX, point.posY, point.posZ, value, count);
    }
}

#endif
